#ifndef __MEMORY__
#define __MEMORY__

/*
Allocates a block of memory aligned at 16 bytes.
Parameter length specifies the minimum length of the memory block.
Parameter base_ptr specifies the original pointer obtained with standard allocation
 (use base_ptr for deallocation).
*/
void *malloc__a16(unsigned int length, void **base_ptr);

// Use this define to avoid copy-pasting the body of operator new overload for each class.
// Operator new is static (non-member), so you cannot inherit functionality from base class.
// CLASS must have the following member:
//   void *base_ptr;
#define OPERATOR_NEW_OVERLOAD(CLASS) void* CLASS::operator new(size_t) \
{ \
	void *base_ptr; \
	void *p = malloc__a16(sizeof(CLASS), &base_ptr); \
	if (p != NULL) \
	{ \
		/* Set object member before the constructor is called
			(which can be incorrect under some circumstances) */ \
		CLASS *t = reinterpret_cast<CLASS*>(p); \
		t->base_ptr = base_ptr; \
		 \
		return p; \
	} \
	else \
	{ \
		throw bad_alloc(); \
	} \
}

void Triple__memlayout_test();

#endif
